home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / compuserve-file-archive / 03 Demos and Info / PUNTER.DOC < prev    next >
Encoding:
Text File  |  2019-04-13  |  23.4 KB  |  511 lines

  1.  
  2. The "C1" Protocol
  3. -----------------
  4.  
  5. The "C1" description is exceedingly long, so it has been broken up into 11
  6. more easily digestible sections. If you haven't read any of this before,
  7. please read them ALL, in order.
  8.  
  9. Topic                            Section
  10. -----                            -------
  11. Inception & Concepts ........... C1-1
  12. A Simple Conversation .......... C1-2
  13. Communication Codes & Checksums. C1-3
  14. Statement and Listen Loops ..... C1-4
  15. Synchonization Lock ............ C1-5
  16. Block Structure ................ C1-6
  17. Varying Block Size ............. C1-7
  18. Communication Syntax ........... C1-8
  19. Syntax Description ............. C1-9
  20. The "Endoff" Situation ......... C1-10
  21. Transfering File Type .......... C1-11
  22.  
  23.  
  24. Section C1-1
  25. ------------
  26.  
  27. Inception
  28. ---------
  29. During the summer of 1981, when I first got the idea of putting up a BBS, I
  30. started work on a simple protocol for transfering programs to and from the
  31. BBS. This protocol was similar in structure to XMODEM, and had about the same
  32. reliability. Under good line conditions, it would give error free transfers
  33. (this was to be expected). Under moderate noise conditions, the protocol would
  34. hold up, and would still give error free transmissions. It was under poor line
  35. conditions that it, and XMODEM, would fall apart.
  36.  
  37. In the summer of 1984, I started work on a very ambitious project; to produce
  38. a protocol that was both fast, and extremely reliable, even under the worst of
  39. line conditions. From this work came the "C1" protocol; not a simple
  40. block/checksum affair, but a complete communication system for the computer.
  41.  
  42. Be warned, therefore, that understanding the ins and outs of "C1" will not be
  43. easy, but with enough patience, there's no reason why even the least skilled
  44. programmer cannot be comfortable with it.
  45.  
  46. Concepts
  47. --------
  48. The concept behind the "C1" protocol was simple; to allow two computers to
  49. "talk" with one another (while transferring data) in such a way that nothing
  50. short of a complete distortion of the transmission line could result in a
  51. misunderstanding. If this concept could be realized, then files could be
  52. transferred between computers without fear of line noise causing a breakdown
  53. in the protocol, or that the received data would differ, in any way, from that
  54. which was sent. Nothing is perfect though, and I don't, for a minute, claim
  55. that "C1" is completely infallible, but I can say, with reasonable comfort,
  56. that "C1" can deliver bad line accuracy not found in any other microcomputer
  57. transfer protocols. For this accuracy though, there is a price to pay, and it
  58. is complexity; the protocol is extremely difficult to duplicate without a
  59. complete and utter understanding of the intricate workings of "C1". This
  60. document will attempt to give you that required understanding.
  61.  
  62.  
  63.  
  64. Section C1-2
  65. ------------
  66.  
  67. A Simple Conversation
  68. ---------------------
  69. In first deciding how the protocol would function, I thought of how two people
  70. could carry on a conversation under high noise conditions, where
  71. misunderstanding would be the norm. The senario I'm going to give differs from
  72. the protocol in that the people talking have no way of verifying the accuracy
  73. of what they believe they have heard. What it is meant to demonstrate is how
  74. the the two computers "talk" with one another, and discuss the neccessary
  75. repetition, or non-repetition, of each block of data (the cornerstone of a
  76. checksum based transfer protocol).
  77.  
  78. Ken and John are attempting to assemble a machine in the middle of a very
  79. noisy machine shop. Ken reads the instructions to John, who carries them out.
  80. Even at close proximity, the two have difficulty hearing one another, so they
  81. adopt of form banter which allows each instuction to be verified and
  82. acknowledged. Here is how the conversation might go:
  83.  
  84. John: Put part "A" in hole "D".
  85.  
  86. Ken: Understood, putting part "A" in hole "D".
  87.  
  88. John: Acknowledged, let me know when you are ready for the next instruction.
  89.  
  90. Ken: Go ahead, what do I do next?
  91.  
  92. John: Put screw "E" through slot "T".
  93.  
  94. Ken: I didn't understand that, could you please repeat.
  95.  
  96. John: Oh, ok, tell me when you're ready for that instruction again.
  97.  
  98. Ken: Ready now.
  99.  
  100. The conversation continues on in this fashion, guaranteeing that both John and
  101. Ken are fully aware of what the other is doing. In real life, people wouldn't
  102. have the patience to keep up that sort of banter, but that's why they make
  103. more mistakes than a computer. It is just this sort of "conversation" that the
  104. two computers have between each other, only the language is different; the
  105. instruction is replaced by the block of data, and all other statements by
  106. special codes.
  107.  
  108.  
  109.  
  110.  
  111. Section C1-3
  112. ------------
  113.  
  114. Communication Codes
  115. -------------------
  116. One of the areas where simple protocols fall apart is in the transmission of
  117. "handshaking codes". It's called handshaking because is implies that the two
  118. computers are having a dialogue, rather than a monologue. These other
  119. protocols rely on single byte (8 bit) words for their communication codes, and
  120. that could spell trouble, since the likelihood of any one 8 bit code being
  121. transposed into another is greater than for multiple byte codes. For this
  122. reason, "C1" uses 3 byte (24 bit) codes which are sufficiently different that
  123. the likelihood of a transposition is extremely low. Not only that, but as you
  124. will soon learn, the method of receiving 3 byte codes is designed such that if
  125. there is sufficient line noise to make the neccessary transpositions, there
  126. would most likely be extra characters sent; "C1" can avoid this situation.
  127.  
  128. Five distinct codes are used in the protocol; "GOO", "BAD", "ACK", "S/B", and
  129. "SYN". Each has it's own meaning, just like any English word, and all are used
  130. in a specific sequence such that synchronization difficulties would be
  131. automatically identified and corrected.
  132.  
  133. Checksums
  134. ---------
  135. When a block of data is sent, we must have a way of determining if it is
  136. correctly received or not. This is accomplished by using what is known as a
  137. checksum. Quite simply, a checksum is a number which is mathematically derived
  138. from all the bytes within the block. The receiving computer recalculates the
  139. sum and compares it with the sum it received along with the block.
  140. Theoretically, any fault in the transmitted data will result in the two
  141. checksums not matching; but that's theory. In reality, the accuracy of the
  142. checksum is based on the type of mathematical operation used to calculate it,
  143. and what kind of noise it encounters.
  144.  
  145. The simplest way to create a checksum is to add up all the ASCII values of the
  146. bytes contained in the block. This is fine for many types of errors, but not
  147. the type which inverts a particular bit. Should two identical inversions occur
  148. on two opposite bits, the sum will remain the same. For example, take the
  149. following two bytes:
  150.  
  151.      11010011 = 211
  152. Plus 01101101 = 109
  153.      --------   ---
  154.                 320
  155.  
  156. Now assume that the forth bit from the right of both of these bytes becomes
  157. inverted by line noise:
  158.  
  159.      11011011 = 219
  160. Plus 01100101 = 101
  161.      --------   ---
  162.                 320
  163.  
  164. As you can see, the sum remains 320, even though line noise has made obvious
  165. changes to the bytes. A better system is one called "Cyclic Redundancy", which
  166. works on a somewhat different principle. The checksum is 16 bits long, and is
  167. created in the following fashion; each byte from the block is Exclusive OR'ed
  168. with the low order part of the checksum. The checksum is then ROTATED one bit
  169. to the left, and the procedure repeated with the next byte.  Even this highly
  170. superior method can be tripped up, so I have combined BOTH an additive
  171. checksum and Cyclic Redundancy checksum to create one very hard to beat 32 bit
  172. "super" checksum.
  173.  
  174.  
  175.  
  176. Section C1-4
  177. ------------
  178.  
  179. Listening For Code Words
  180. ------------------------
  181. Although 3 byte code words are more reliable than 1 byte code words, nothing
  182. is perfect. It was once said that if you let an infinite number of monkeys
  183. bash away at typewriters for an infinite amount of time, one of them would
  184. eventually type "To be or not to be, that is the question". Although this
  185. stretches statistical probability to it's limit, this kind of thing can easily
  186. happen on a smaller scale; the letters "GOO" could quite conceivably be
  187. produced by purely random line noise.
  188.  
  189. To try and eliminate ALL possible errors isn't feasible, but "C1" makes an
  190. attempt at trying to eliminate as many as possible. One reasonably probable
  191. fact is that any noise capable of randomly producing "GOO", would not stop
  192. there; more likely, it would produce a string of characters, something like
  193. "HGOOEK". Were we to allow the protocol to listen exclusively for three letter
  194. combinations, it would most assuredly pick out the "GOO" in that string.
  195.  
  196. My specifications for "C1" call for a code recognition routine which will ONLY
  197. make code word comparisons on the LAST 3 RECEIVED bytes. This is accomplished
  198. in my coding by going back and testing for further characters after I have
  199. identified a three byte code word. Should another byte be present, the
  200. identified code word is thrown away, and the search will continue.
  201.  
  202. Statement and Listen Loops
  203. --------------------------
  204. One immediate drawback to the system described above is that a REAL code word,
  205. masked within some random noise, would be rejected by the receiving computer.
  206. This would also be true of a code word simply damaged by noise (like "GOE").
  207. For a protocol to be impervious to this sort of corruption, it must be capable
  208. of restating code words over and over until the receiving computer can
  209. understand, yet it must also have a way of knowing whether the receiving
  210. computer got the code word or not. This was a fact that eluded me when I wrote
  211. the original protocol. When we talk to other people, the cornerstone of
  212. understanding is recognition. If we ask "What do you think?", yet get no
  213. reply, we ask again. Only when we receive a reply from the person to whom we
  214. are talking do we continue on with our next statement. It would be pointless
  215. wasting our breath on someone who isn't listening.
  216.  
  217. Within "C1", communication between computers is handled through a similar
  218. system which I call the "Statement and Listen Loop". It's quite simple really;
  219. when one computer has to "say" something to the other, it does so, then waits
  220. for a predetermined time for a known response. Should it fail to receive a
  221. response within that period of time, the code word is said again, and the
  222. computer listens for the reply. This continues until the required response is
  223. heard. The system is further enhanced by the fact that both computers are
  224. ALWAYS engaged in a "Statement and Listen Loop".
  225.  
  226.  
  227.  
  228. Section C1-5
  229. ------------
  230.  
  231. Synchronization Lock
  232. --------------------
  233. That rather ominous sounding title is actually rather simple; it refers to a
  234. condition whereby the "Statement and Listen Loops" of each computer become
  235. locked together. This is analogous to two people speaking at the same time,
  236. over and over, such that no effective communication takes place. In order to
  237. guarantee that the two computers never get into this state, the wait times of
  238. the loops are altered slightly.
  239.  
  240. Assume that the fixed wait loop time was 0.5 seconds; this is called a "Short"
  241. wait. We also have a "Long" wait, which would be slightly longer, say 0.6
  242. seconds (actually, the delay within a "Statement and Listen Loop" is not
  243. particually critical, but should be somewhere in the neighbourhood of one half
  244. second). Each time the computer goes through an SLL, a counter would determine
  245. which type of wait to use; Long or Short. The sequence is broken into three;
  246. the transmitting computer will use a Long-Long-Short, while the receiving
  247. computer will use a Short-Short-Long.
  248.  
  249.  
  250.  
  251. Section C1-6
  252. ------------
  253.  
  254. Block Structure
  255. ---------------
  256. Each block of data contains somewhat more than just a collection of characters
  257. taken from disk, it also contains a "header". The header is 7 bytes long, and
  258. contains the following information:
  259.  
  260. Byte 1: Low part of ADDITIVE checksum
  261. Byte 2: High part of ADDITIVE checksum
  262. Byte 3: Low part of CLC checksum
  263. Byte 4: High part of CLC checksum
  264. Byte 5: Size of NEXT block
  265. Byte 6: Low part of Block Number
  266. Byte 7: High part of Block Number
  267.  
  268. As you remember from the section on "checksums", there are two distinctly
  269. different, 16 bit (2 byte) checksums. One is an additive checksum, composed of
  270. the mathematical sum of the ASCII values of all the DATA bytes (and bytes 5
  271. through 7 of the header). The other checksum is calculated using Cyclic (CLC)
  272. Redundancy (on the same bytes). These 32 checksum bits are placed in the first
  273. 4 bytes of the header.
  274.  
  275. The 5th byte is the length of the NEXT block. This may seem odd to some, but
  276. consider the difficulties in sending the size of the current block in that
  277. self same block. You need to know the block size to calculate the checksum,
  278. but you can't know for sure that the block size is correct unless you have
  279. verified the checksum. We call this a Catch-22. By sending the size of any
  280. given block in the PREVIOUS block, the size is known for a fact BEFORE the
  281. checksum is calculated.
  282.  
  283. In the 6th and 7th byte are the block number. This was added quite early on in
  284. the development of "C1" under the assumption that it would be necessary (as it
  285. is in XMODEM). As it turned out, "C1" uses a method of handshaking which makes
  286. this unnecessary. None the less, my specifications call for it's inclusion, as
  287. certain uses of the block number could be made. Also, the high order part of
  288. the block number (byte 7 of the header) is used to flag the last block.
  289.  
  290.  
  291.  
  292. Section C1-7
  293. ------------
  294.  
  295. Varying Block Size
  296. ------------------
  297. The reason that block size was included in the header was originally to allow
  298. the last block only to vary in size (one can never guarantee that the amount
  299. of data to be sent will divide nicely into a preset block size). It quickly
  300. dawned on me that "C1" was set up in such a way that ANY block size could be
  301. used for ANY block in the transmission. Varying block size has it's
  302. advantages; under reasonably clean line conditions, large blocks transmit the
  303. most data with the least handshaking (which is mildly time consuming). Smaller
  304. blocks are superior under bad noise conditions, since smaller blocks run a
  305. higher chance of making it through the noise unscathed; and should it still
  306. fail to make it, less time is required to repeat a smaller block.
  307.  
  308. My current implementation of "C1" allows the user to pick a fixed block size
  309. between 40 and 255 bytes, but in other implementations, there is no reason why
  310. block size couldn't be varied DURING transmission to adapt to CHANGING line
  311. conditions.
  312.  
  313. One final thing concerning block structure is how would one presume to know
  314. the size of the FIRST BLOCK if that is revealed only in the block that came
  315. before it (quite a paradox). "C1" requires that the first block contain ONLY a
  316. header, which would make that block 7 bytes long. This header would do little
  317. more than supply the receiving computer with the size of first REAL block.
  318. Accuracy of this first "dummy" block is guaranteed since it must still pass
  319. the checksum tests. You must make the block number for this dummy block "0".
  320.  
  321.  
  322.  
  323.  
  324. Section C1-8
  325. ------------
  326.  
  327. Communication Syntax
  328. --------------------
  329. Now that you understand block structure, handshaking methods, and code word
  330. vocabulary, it comes time to find out how this all comes together. Most
  331. procotols have very simple handshaking between blocks which is easy to trip
  332. up, given sufficiently noisy conditions. Usually, the transmitting computer
  333. sends the block, then waits for a response from the receiving computer; either
  334. "good" or "bad". The transmitting computer then proceeds to send the next
  335. block (if "good") or resend the last block (if "bad"). This system falls apart
  336. the moment the transmitting computer receives a false indication of "good" or
  337. "bad" and goes on to transmit the wrong block (and whether the receiving
  338. computer likes it or not, it has to tackle with another block). Should things
  339. get out of sync, and the transmitting computer sends the next block when it
  340. should have sent the last one again, XMODEM attempts to make corrections by
  341. use of the block number encoded within each block.
  342.  
  343. "C1" does nothing so crude; it's very communication syntax guarantees that
  344. neither computer will get out of phase with the other. Whereas XMODEM uses a
  345. single statement monologue between each block, "C1" uses a multiple part
  346. dialogue. This makes "C1" about 3% slower than XMODEM, but this small
  347. trade-off in speed for accuracy will be well worth it the first time you run
  348. into trouble with XMODEM.
  349.  
  350. XMODEM commincations would look something like this:
  351.  
  352. Xmit: Transmits Block
  353. Rec : "Good"
  354. Xmit: Transmits Next Block
  355. Rec : "Bad"
  356. Xmit: Transmits Same Block Again
  357.  
  358. In "C1", the transmission would look something like this:
  359.  
  360. Xmit: Transmits Block
  361. Rec : "Good"
  362. Xmit: Good block acknowledged
  363. Rec : Send next block for me
  364. Xmit: Transmits Next Block
  365. Rec : "Bad"
  366. Xmit: Bad block acknowledged
  367. Rec : Send that block again
  368. Xmit: Transmits Same Block Again
  369.  
  370. In this type of transmission dialogue, neither computer can get out of sync,
  371. since should it receive the opposite response than it expects, it goes back to
  372. give the correct code word for the response it DID RECEIVE, thus regaining
  373. proper synchronization. Couple this with the "Statement and Listen Loops", and
  374. you can readily see than communication would be hard to break down.
  375.  
  376.  
  377.  
  378.  
  379. Section C1-9
  380. ------------
  381.  
  382. Syntax Description
  383. ------------------
  384. The following diagram should give you an understanding of the flow of
  385. information between blocks:
  386.  
  387. For a Good Block:
  388.  
  389. Xmit: [Block]   "ACK"   [Next Block]
  390. Rec :       "GOO"   "S/B"
  391.  
  392. For a Bad Block:
  393.  
  394. Xmit: [Block]   "ACK"   [Same Block]
  395. Rec :       "BAD"   "S/B"
  396.  
  397. Actually, the two are identical; the only difference is the substitution of
  398. either "GOO" or "BAD" as the response to the received block. Immediately after
  399. receiving the block, the receiving computer recalculats the checksum to
  400. determine validity of the data. In the meantime, the transmitting computer
  401. starts to wait for a "GOO" or "BAD" signal. Since it can "say" nothing until
  402. it receives one of these codes, it merely waits. That may sound suspiciously
  403. like a good place to "hang up" the protocol, but the receiving end is
  404. eventually going to finish receiving the block, either because it timed out
  405. waiting, or it finished collecting the correct number of bytes from the
  406. transmitting computer.
  407.  
  408. At that time, the receiving computer sends the appropriate code word ("GOO" or
  409. "BAD") and begins to wait for an acknowledgement ("ACK"). If it doesn't
  410. receive the "ACK" in about one half second, it sends the "GOO" or "BAD" code
  411. word once again. Meanwhile, the transmitting computer has been patiently
  412. awaiting the reception of the "GOO" or "BAD" code. Once it receives it, it
  413. transmits an "ACK" and starts to wait for an "send block" signal ("S/B"). If
  414. it doesn't get the "S/B" within about one half second, it sends "ACK" again.
  415.  
  416. Back at the receiving computer, which is waiting for this "ACK" signal, it
  417. receives it and sends the "S/B" signal and begins to wait for the block.
  418. Should it receive an "ACK" while waiting for the block, or receives nothing at
  419. all for approximately .5 seconds, it assumes that the transmitting computer
  420. hasn't heard the "S/B" and transmits it again. In the meantime, the
  421. transmitting computer is waiting for the "S/B", and upon reception, starts
  422. sending the block. The process has now started all over again.
  423.  
  424. A quick analysis of this system will reveal that it's damned near impossible
  425. to get any type of noise which could possibly mimick the code sequences
  426. required. Also, no noise could stop the eventual completion of the above
  427. sequence, since each computer is aways "sending and waiting". If two people
  428. keep repeating their sentences over and over, and continue to listen to the
  429. other person, even a noisy room couldn't stop them from hearing one another
  430. EVENTUALLY. Of course, some line noise is just so horrendous, that even this
  431. method of communication could fail. Then again, this type of noise would make
  432. it damned near impossible for the user to be online in the first place, so it
  433. can be considered an unlikely event. But, should one of the computers go
  434. offline for any reason, we wouldn't want the other computer to keep looping
  435. and looping until it died of old age.
  436.  
  437. Although I haven't built in such protection into the terminal program I
  438. distribute in the public domain, my BBS program does have abortion code.
  439. Should the protocol on the BBS have to go through the "Statement and Listen
  440. Loop" more than 24 times in row (which is hightly unlikely if the other
  441. computer is still online), it will abort the transfer. Similar code could be
  442. used in your implementation.
  443.  
  444.  
  445.  
  446.  
  447. Section C1-10
  448. -------------
  449.  
  450. The End-Off Situation
  451. ---------------------
  452. When the final block is transmitted, the high order part of the block number
  453. should be made HEX "FF" (255 decimal). This will inform the receiving computer
  454. that this is the last block of data, and to expect no more. The question now
  455. arises; how can both computers be 100% sure that the other is fully aware of
  456. the file completion? A fair question, but not one with a simple answer.
  457.  
  458. When the transmitting computer receives the "GOO" for the last block, it can
  459. be fairly certain that the receiving computer has received the final block,
  460. but it must inform the receiving computer that it knows this. It does so by
  461. sending an "ACK", but cannot be sure the receiving computer has received the
  462. "ACK" unless it gets the "S/B" signal back. Now, the transmitting computer
  463. must acknowledge the reception of the "S/B", but under the normal
  464. communications syntax, it would now have send a block. This is where the
  465. "End-Off" syntax comes into play; after receiving the "S/B", the transmitting
  466. computer sends back a "SYN" signal. In response to that receiving computer
  467. sends it's own "S/B" signal, then waits for the final "S/B" from the
  468. transmitting computer. Since it will not be responding to this code, it simply
  469. goes into a wait cycle for approximately 5 seconds.
  470.  
  471. If it does get the "S/B" within that 5 seconds, it ends immediately, but
  472. otherwise doesn't really care if it receives the code or not since at this
  473. stage, there is a 100% assurance of both computers knowing things are Ok. The
  474. transmitting computer need only send three copies of the "S/B" code at this
  475. point, since, as stated above, there is full assurance that both computers are
  476. finished. NOTE that the code words chosen for the End-Off situation are not
  477. necessarily related to their appearant function.
  478.  
  479.  
  480.  
  481. Section C1-11
  482. -------------
  483.  
  484. Transfering File Type
  485. ---------------------
  486. When transfering files from one computer to another it is often necessary to
  487. also transfer the file type, but this must be known BEFORE the file is opened,
  488. and, therefore, before the protocol begins. "C1" does not impose any strict
  489. rules on what sort of information you transfer about the files, if any, but
  490. when writing a terminal program to communicate with one of my bulletin boards,
  491. the following should be done:
  492.  
  493. Using a full implementation of the "C1" procotol (first dummy block, data
  494. block, and End-Off), transmit a single byte of data corresponding to the
  495. following file types:
  496.  
  497. 1 = Program File
  498. 2 = SEQ File
  499. 3 = WordPro File
  500.  
  501. Transmitting this single piece of data would require that TWO blocks be sent;
  502. the initial dummy block to set up the size of the first data block (of which
  503. there will be only one, size 8), and the data block itself, consisting of 7
  504. header bytes and the single file type byte. For other applications, one could
  505. conceivable transfer much more information, including file name, file type,
  506. computer type, etc. It could even be possible to transfer multiple files,
  507. specifying the number and name of each file in this first transmission.
  508. Alternately, no one said you HAVE to use this first separate transmission; if
  509. no information other the file needs to be transmitted, you just send the file
  510. and nothing more.
  511.